package com.kyriec.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kyriec.Util.EResult;
import com.kyriec.entity.Carts;
import com.kyriec.mapper.CartsMapper;
import com.kyriec.service.CartsService;
import com.kyriec.service.GoodsService;
import com.kyriec.vo.CartVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 购物车(Carts)表服务实现类
 *
 * @author KyrieC
 * @since 2023-11-28 11:02:02
 */
@Service("cartsService")
public class CartsServiceImpl extends ServiceImpl<CartsMapper, Carts> implements CartsService {

    @Autowired
    private CartsMapper cartsMapper;

    @Autowired
    private GoodsService goodsService;

    @Override
    public EResult getCartsInfoByUserId(Integer userid) {
        LambdaQueryWrapper<Carts> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Carts::getUserId,userid);
        List<Carts> cartsList =  cartsMapper.selectList(queryWrapper);
        List<CartVo> cartVoList = new ArrayList<>();
        for(Carts carts : cartsList){
            cartVoList.add(new CartVo(carts,goodsService.getById(carts.getGoodId())));
        }
        return EResult.success(cartVoList);
    }

    @Override
    public EResult deleteCartsById(Integer id) {
        cartsMapper.deleteById(id);
        return EResult.success();
    }

    @Override
    public EResult addCarts(Carts carts) {
        cartsMapper.insert(carts);
        return EResult.success();
    }

}

